package com.miaoshaproject.lender;

import com.miaoshaproject.lender.credit.CreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description
 * @Author wuqiusheng
 * @Version 1.0
 * @Date 2025/6/4 22:35
 */
@Component
public class ServiceLocator {

    private final Map<String, CreditService> routerMap = new ConcurrentHashMap<>();

    @Autowired
    public ServiceLocator(List<CreditService> creditServices) {
        initRouterMap(creditServices);
    }

    private void initRouterMap(List<CreditService> services) {
        services.forEach(service -> {
            StrategySPI anno = service.getClass().getAnnotation(StrategySPI.class);
            if (anno != null) {
                routerMap.put(anno.tenantId() + "_" + anno.lenderCode(), service);
            }
        });
        System.out.println("ServiceLocator routerMap:" + routerMap);
    }

    public CreditService route(String site, String funder) {
        return routerMap.get(site + "_" + funder);
    }

    public CreditService route(String routeKey) {
        return routerMap.get(routeKey);
    }
}
